package com.example.kliven.because.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.kliven.because.R;
import com.example.kliven.because.Singleton.VolleySingleton;
import com.example.kliven.because.adapter.FashionAdapter;
import com.example.kliven.because.bean.FashionBean;
import com.example.kliven.because.intentbean.NetFashionBean;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kliven on 16/1/11.
 */
public class FashionFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    private ListView listView;
    private List<FashionBean> fashionBeans;
    private FashionAdapter fashionAdapter;
    private SwipeRefreshLayout swipeRefreshLayout;
    private String url = "http://zzd.sm.cn/appservice/api/v1/channel/1213442674?client_os=android&client_version=2.1.0.1&bid=800&m_ch=005&city=0411&sn=eda7198b96b6b6239a7f5b39114635e0&content_cnt=20&recoid=10910363202736418410&ftime=1452839956667&method=new&count=20";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fashionfragment,null);
        listView = (ListView) view.findViewById(R.id.list_view);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh_layout);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        swipeRefreshLayout.setOnRefreshListener(this);
        VolleySingleton.getInstance();
        getNetData();
        return view;
    }

    private void getNetData() {
        VolleySingleton.addRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                fashionBeans = new ArrayList<>();
                Gson gson = new Gson();
                NetFashionBean netFashionBean = gson.fromJson(response,NetFashionBean.class);
                for (int i = 0; i < netFashionBean.getData().getArticle().size(); i++) {
                    FashionBean fashionBean = new FashionBean();
                    fashionBean.setTitle(netFashionBean.getData().getArticle().get(i).getTitle());
                    for (int j = 0; j < netFashionBean.getData().getArticle().get(i).getImage().size(); j++) {
                        fashionBean.setMain_img_url(netFashionBean.getData().getArticle().get(i).getImage().get(j).getUrl());
                    }
                    fashionBeans.add(fashionBean);
                }
                fashionAdapter = new FashionAdapter(getActivity(),fashionBeans);
                listView.setAdapter(fashionAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                
            }
        });
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(false);
                getNetData();
            }
        },5000);
    }
}
